<template>
  <div class="main-content" style="width: 50%">
    <img :src="works.cover" alt="" style="width: 100%; border-radius: 5px">
    <div style="margin: 20px 0; font-size: 22px; font-weight: bold">名称:{{ works.name }}</div>
    <div style="display: flex; margin-bottom: 30px">
      <div style="display: flex; align-items: center;">
        <img :src="works.avatar" alt="" style="width: 30px; height: 30px; border-radius: 50%">
        <span style="margin-left: 5px">{{ works.userName }}</span>
      </div>
      <div style="flex: 1; display: flex; align-items: center; justify-content: flex-end">
        <span style="font-size: 20px; color: orange; font-weight: bold; margin-right: 2px">{{ works.count }}</span> 浏览
        <span style="font-size: 20px; color: orange; font-weight: bold; margin-right: 2px; margin-left: 30px">{{
            works.collectCount
          }}</span> 收藏
        <span style="font-size: 20px; color: orangered; font-weight: bold; margin-right: 2px; margin-left: 30px">{{
            works.likesCount
          }}</span> 点赞

        <el-button icon="el-icon-like" style="margin-left: 10px; color: orange" v-if="works.userLike" @click="like">
          已点赞
        </el-button>
        <el-button icon="el-icon-unlike" style="margin-left: 10px" v-else @click="like">点赞</el-button>

        <el-button icon="el-icon-star-on" style="margin-left: 10px; color: orange" v-if="works.userCollect"
                   @click="collect">已收藏
        </el-button>
        <el-button icon="el-icon-star-off" style="margin-left: 10px" v-else @click="collect">收藏</el-button>

        <el-button icon="el-icon-warning-outline" style="margin-left: 10px; color: orange" v-if="works.userReport"
                   @click="handleReportExist">已举报
        </el-button>
        <el-button icon="el-icon-warning" style="margin-left: 10px" v-else @click="openReportDialog">举报</el-button>

      </div>
    </div>
    <div style="margin: 20px 0; font-size: 18px; font-weight: bold">作品简介</div>
    <div style="font-size: 14px; color: #666; line-height: 24px; margin: 20px 0">{{ works.descr }}</div>
    <div style="margin: 20px 0; font-size: 18px; font-weight: bold">作者简介</div>
    <div style="font-size: 14px; color: #666; line-height: 24px; margin: 20px 0">{{ works.author }}</div>
    <div style="margin: 20px 0; font-size: 18px; font-weight: bold">创作背景</div>
    <div style="font-size: 14px; color: #666; line-height: 24px; margin: 20px 0">{{ works.background }}</div>
    <div style="margin: 30px 0">
      <div style="margin: 20px 0; font-size: 18px; font-weight: bold">内容介绍</div>
      <div style="display: flex; margin-bottom: 10px" v-for="(item, index) in contentList" :key="index">
        <img :src="item.img" style="width: 100px; height: 100px">
        <div style="padding-left: 10px">
          <div style="font-size: 18px; font-weight: bold; margin-bottom: 10px">第{{ index + 1 }}部分</div>
          <div>{{ item.text }}</div>
        </div>
      </div>
    </div>
    <div style="margin: 30px 0">
      <div style="margin: 20px 0; font-size: 18px; font-weight: bold">人物介绍</div>
        <el-table
            :data="characterList"
            height="250"
            border
            style="width: 100%; margin-top: 10px">
          <el-table-column
              prop="name"
              label="人物名称"
              width="150"
              header-align="center"
              :header-style="{ fontSize: '16px' }" >
          </el-table-column>
          <el-table-column
              prop="text"
              label="人物简介"
              header-align="center"
              :header-style="{ fontSize: '16px' }" >
          </el-table-column>
        </el-table>
    </div>
    <div style="margin: 30px 0">
      <div style="margin: 20px 0; font-size: 18px; font-weight: bold">作品赏析</div>
      <div style="display: flex; margin-bottom: 10px" v-for="(item, index) in appreciationList" :key="index">
        <div >
          <div style="font-size: 18px; font-weight: bold; margin-bottom: 10px">{{item.name}}</div>
          <div>{{ item.text }}</div>
        </div>
      </div>
    </div>
    <div style="margin: 30px 0">
      <div style="margin: 20px 0; font-size: 18px; font-weight: bold">造成影响</div>
      <div style="font-size: 15px">{{ works.effect }}</div>
    </div>
    <div style="margin: 30px 0">
      <div style="margin: 20px 0; font-size: 18px; font-weight: bold">资讯消息</div>
      <el-table
          :data="infoList"
          height="250"
          border
          style="width: 100%;margin-top: 10px">
        <el-table-column
            prop="time"
            label="资讯发布时间"
            width="150"
            header-align="center"
            :header-style="{ fontSize: '16px' }" >
        </el-table-column>
        <el-table-column
            prop="text"
            label="资讯内容"
            width=""
            header-align="center"
            :header-style="{ fontSize: '16px' }" >
        </el-table-column>
      </el-table>
    </div>

    <div style="margin: 30px 0">
      <div style="margin: 20px 0; font-size: 18px; font-weight: bold">出版时间:{{works.publishTime}}</div>
    </div>

    <div style="margin: 30px 0; color: #666; font-size: 12px">帖子发布时间:{{ works.releaseTime }}</div>
    <div style="margin: 30px 0; color: #666; font-size: 12px">帖子上一次更新时间:{{ works.lastUpdateTime }}</div>

    <div style="margin: 30px 0" v-if="tagList&&tagList.length">
      <el-tag style="margin-right: 10px; font-family: 'Microsoft YaHei'; cursor:pointer; font-size: 12px" v-for="tag in tagList" :key="tag" @click="handleTag(tag)">#{{ tag }} </el-tag>
    </div>

    <div style="margin: 50px 0">
      <Comment :fid="id" module="文艺作品"/>
    </div>

    <el-dialog :visible.sync="dialogFormVisible" title="举报" width="30%">
      <el-form :model="reportForm" :rules="rules" ref="formRef" label-width="80px" style="padding: 0 20px" status-icon >
        <el-form-item label="举报内容" prop="content">
          <el-input type="textarea" placeholder="请输入举报内容" v-model="reportForm.content"
                    autocomplete="off"></el-input>
        </el-form-item>
        <span style="display: flex;justify-content: center;margin-top: 10px">
        <el-button @click="dialogFormVisible = false">取消</el-button>
        <el-button type="primary" @click="report">
          举报
        </el-button>
      </span>
      </el-form>
    </el-dialog>

  </div>
</template>

<script>
import '@/assets/css/iconfont/iconfont.css'
import Comment from "@/components/Comment";

export default {
  name: "WorkDetail",
  components: {Comment},
  data() {
    return {
      id: this.$route.query.id,  // 文艺作品ID
      works: {},
      materialList: [],
      contentList: [],
      characterList:[],
      appreciationList:[],
      infoList:[],
      tagList:[],
      user: JSON.parse(localStorage.getItem('xm-user') || '{}'),
      dialogFormVisible: false,
      reportForm: {},
      rules: {
        content: [
          {required: true, message: '请输入举报内容', trigger: 'blur'}
        ]
      }
    }
  },
  created() {
    this.updateCount()
    this.loadComment()
    this.loadTag()
  },
  mounted() {
  },
  methods: {
    updateCount() {
      this.$request.put('/works/updateCount/' + this.id).then(res => {
        this.load()
      })
    },
    like() {  // 点赞
      this.$request.post('/likes/set', {fid: this.id, userId: this.user.id, module: '文艺作品'}).then(res => {
        if (res.code === '200') {
          this.$message.success('点赞成功')
          this.load()
        } else {
          this.$message.error(res.msg)
        }
      })
    },
    collect() {
      this.$request.post('/collect/set', {fid: this.id, userId: this.user.id, module: '文艺作品'}).then(res => {
        if (res.code === '200') {
          this.$message.success('收藏成功')
          this.load()
        } else {
          this.$message.error(res.msg)
        }
      })
    },
    handleReportExist(){
      this.$message.warning('您已经举报过了')
    },
    report() {
      this.$refs['formRef'].validate((valid) => {
        if (valid) {
          this.$request.post('/report/set', {
            fid: this.id,
            userId: this.user.id,
            module: '文艺作品',
            content: this.reportForm.content
          }).then(res => {
                if (res.code === '200') {
                  this.$message.success('举报成功')
                  this.dialogFormVisible = false
                  this.load()
                } else {
                  this.$message.error(res.msg)
                }
              }
          )
        } else {
          this.$message.error(res.msg)
        }
      })
    },
    openReportDialog() {
      this.dialogFormVisible = true;
    },
    loadTag() {
      this.$request.get('/tag/selectAll').then(res => {
        this.tagList = res.data || []
      })
    },
    load() {
      this.$request.get('/works/selectById/' + this.id).then(res => {
        this.works = res.data || {}
        this.characterList = JSON.parse(this.works?.chara || '[]')
        this.contentList = JSON.parse(this.works?.content || '[]')
        this.appreciationList=JSON.parse(this.works?.appreciation || '[]')
        this.infoList=JSON.parse(this.works?.workInfo||'[]')
        this.tagList=JSON.parse(this.works?.tags||'[]')
      })
    },
    handleTag(searchKeyWord){
      window.open('/front/works?searchKeyword=' + searchKeyWord)
    }
  }
}

</script>

<style scoped>
.br {
  border-right: 1px solid #ccc;
}

.bt {
  border-bottom: 1px solid #ccc;
}

.comment-active {
  color: #2a60c9;
}
</style>